package com.zry.ai.mapper;
import com.zry.ai.entity.ChatMemoryEntity.ChatMessageEntity;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface MessageMapper {
    @Insert({
        "<script>",
        "INSERT INTO chat_messages (conversation_id, message_type, content, created_at)",
        "VALUES ",
        "<foreach collection='messages' item='msg' separator=','>",
        "(#{msg.conversationId}, #{msg.messageType}, #{msg.content}, #{msg.createdAt})",
        "</foreach>",
        "</script>"
    })
    void insertMessages(@Param("messages") List<ChatMessageEntity> messages);

    @Select("SELECT * FROM chat_messages " +
            "WHERE conversation_id = #{conversationId} " +
            "ORDER BY created_at DESC " +
            "LIMIT #{lastN}")
    List<ChatMessageEntity> findLastNMessages(@Param("conversationId") String conversationId, 
                                            @Param("lastN") int lastN);

    @Delete("DELETE FROM chat_messages WHERE conversation_id = #{conversationId}")
    void deleteByConversationId(String conversationId);
}